package com.bwei.mypaot.home;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.mypaot.R;
import com.bwei.mypaot.base.activity.BaseActivity;
import com.bwei.mypaot.home.adapter.TypeAdapter;
import com.bwei.mypaot.view.PickerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/2/26.
 */

public class DanActivity extends BaseActivity {

    private String typename;
    private ImageView fhphone;
    private ImageView shphone;
    private TextView tv_namecard;
    private TextView type;
    private TextView tv_money;
    private EditText et_sm;
    private Button bt_smyy;
    private Button bt_tjdd;
    private Button bt_native;
    private Button bt_positive;
    private RecyclerView gridView;
    private ArrayList<String> data;
    private TypeAdapter typeAdapter;
    String typedata;
    private EditText et_fhname;
    private EditText et_fhphone;
    private EditText et_shname;
    private EditText et_shphone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.downdan_activity);
        initHeader();
        initWidget();
        setWidgetState();


    }

    @Override
    public void initHeader() {

    }

    @Override
    public void initWidget() {
        fhphone = (ImageView) findViewById(R.id.fhphone);
        shphone = (ImageView) findViewById(R.id.shphone);
        bt_smyy = (Button) findViewById(R.id.bt_smyy);
        tv_namecard = (TextView) findViewById(R.id.tv_namecard);
        type = (TextView) findViewById(R.id.type);
        tv_money = (TextView) findViewById(R.id.tv_money);
        et_sm = (EditText) findViewById(R.id.et_sm);
        bt_tjdd = (Button) findViewById(R.id.bt_tjdd);
        et_fhname = (EditText) findViewById(R.id.et_fhname);
        et_fhphone = (EditText) findViewById(R.id.et_fhphone);
        et_shname = (EditText) findViewById(R.id.et_shname);
        et_shphone = (EditText) findViewById(R.id.et_shphone);
    }

    @Override
    public void setWidgetState() {
        fhphone.setOnClickListener(this);
        shphone.setOnClickListener(this);
        bt_smyy.setOnClickListener(this);
        tv_namecard.setOnClickListener(this);
        type.setOnClickListener(this);
        tv_money.setOnClickListener(this);
        et_sm.setOnClickListener(this);
        bt_tjdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.fhphone:
                Intent intentfh=new Intent(DanActivity.this,Contacts.class);
                startActivityForResult(intentfh,1);
                break;
            case R.id.shphone:
                Intent intentsh=new Intent(DanActivity.this,Contacts.class);
                startActivityForResult(intentsh,2);
                break;
            case R.id.bt_smyy:

                break;
            case R.id.tv_namecard:

                break;
            case R.id.type:
                data = new ArrayList<String>();
                data.add("休闲食品");
                data.add("生鲜果蔬");
                data.add("办公/居家");
                data.add("其他");
                data.add("鲜花");
                data.add("蛋糕");
                data.add("大件物品");

                View typeview= View.inflate(DanActivity.this,R.layout.type_activity,null);
                final PopupWindow popWnd = new PopupWindow(DanActivity.this);
                popWnd.setContentView(typeview);
                popWnd.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
                popWnd.setFocusable(true);
                popWnd.showAtLocation(view, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);
                //分
                bt_native = (Button) typeview.findViewById(R.id.bt_native);
                bt_positive = (Button) typeview.findViewById(R.id.bt_positive);
                gridView = (RecyclerView) typeview.findViewById(R.id.grid_view);
                gridView.setLayoutManager(new GridLayoutManager(DanActivity.this,3));

                typeAdapter = new TypeAdapter(DanActivity.this, data);
                gridView.setAdapter(typeAdapter);

                typeAdapter.setOnItemClickListener(new TypeAdapter.OnRecyclerViewItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                         type.setBackgroundColor(Color.BLUE);
                         typedata=data.get(position);
                        Toast.makeText(DanActivity.this,data.get(position),Toast.LENGTH_SHORT).show();

                    }
                });

                bt_native.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWnd.dismiss();
                    }
                });
                bt_positive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                   {
                            type.setText(typedata);
                            popWnd.dismiss();
                        }

                    }
                });

                break;
            case R.id.tv_money:

                break;
            case R.id.et_sm:

                break;
            case R.id.bt_tjdd:

                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==1&&resultCode==2){

            String phone=data.getStringExtra("number");
            String name=data.getStringExtra("name");
            et_fhname.setText(name);
            et_fhphone.setText(phone);
        }
    }
}
